﻿class Solution {
public:
    string longestPalindrome(string s) {
        int len = 0, pos = 0;
        for (int i = 0; i < s.size(); i++)
        {
            int left = i, right = i;//先做奇数长度的寻找
            while (left >= 0 && right < s.size() && s[left] == s[right])
            {
                left--;
                right++;
            }
            if (right - left - 1 > len)//注意当结束时left和right还会再走一次
            {
                len = right - left - 1;
                pos = left + 1;
            }

            left = i, right = i + 1;//再做偶数长度的寻找
            while (left >= 0 && right < s.size() && s[left] == s[right])
            {
                left--;
                right++;
            }
            if (right - left - 1 > len)
            {
                len = right - left - 1;
                pos = left + 1;
            }
        }
        return s.substr(pos, len);
    }
};